package com.mastercard.labs

import static org.junit.Assert.*
import org.junit.*

class VoucherServiceTests {

	def voucherService
	
    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

    @Test
    void testSearchByCondition() {
        def searchTitle = 'Promotion'
		def	searchCountry = 1 
		def	searchMerchant = 1
		def cat1 = Category.get(8)
		
		def	searchCategories = 8
		def	searchDate = new Date()
		
		log.info 'voucher search begin:'
		
		def voucherInstanceList = voucherService.searchByCondition(
			searchTitle, 
			searchCountry, 
			searchMerchant, 
			searchCategories, 
			searchDate);
		
		for (Voucher voucher in voucherInstanceList) {

			log.info '###voucher:' + voucher
			
			assertTrue (voucher.title.indexOf(searchTitle) > -1)
				
			assertEquals(searchMerchant, voucher.merchant.id)
			
			assertEquals(searchCountry, voucher.country.id)
			
			assertTrue (cat1 in voucher.categories)
			
			assertTrue ( !searchDate.before(voucher.startDate))
			
			assertTrue ( !searchDate.after(voucher.endDate))
		}
		log.info 'voucher search end'
		
    }
	
	@Test
	void testSearchVoucherForDashboard() {
		def voucherInstanceList = voucherService.searchVoucherForDashboard()
		// Test the size of voucher list must be less than 5
		log.info 'size of voucher list:' + voucherInstanceList.size()
		
		assertTrue (voucherInstanceList.size <= 5)
		
		// Test the top 5 voucher start date 
		def startDate = voucherInstanceList.getAt(voucherInstanceList.size() -1).getStartDate()
		
		def result = Voucher.createCriteria().list {
			gt("startDate", startDate)
		}
		
		assertTrue (result.size <= 5)
	}
}
